//
//  ScanViewController.m
//  text
//
//  Created by 我的 on 16/6/23.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ScanViewController.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "Z_PopView.h"
#import "SVProgressHUD.h"
@interface ScanViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    NSInteger btnSender;
    NSInteger index;
    
    NSArray *typeArr;
    NSArray *moneyArr;
}

@property (nonatomic,strong) NSDictionary *loadUserData;
@property (weak, nonatomic) IBOutlet UIButton *ZhangHUBtn;
@property (weak, nonatomic) IBOutlet UIButton *JiFenBtn;
@property (weak, nonatomic) IBOutlet UILabel *ZhangHuyuELabel;
@property (weak, nonatomic) IBOutlet UILabel *JiFenLabel;
// 备注
@property (weak, nonatomic) IBOutlet UITextField *BeiZhuTF;
// 金额
@property (weak, nonatomic) IBOutlet UITextField *ZhiFuTF;
//密码
@property (weak, nonatomic) IBOutlet UITextField *PassWordTF;
@property (weak, nonatomic) IBOutlet UILabel *ShowLabel;
@property (strong, nonatomic) Z_PopView *popView;
@property (nonatomic, assign) BOOL isSelected;
@property (weak, nonatomic) IBOutlet UIButton *choseBtn;
@end

@implementation ScanViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    typeArr =@[@"男",@"女",@"儿童",@"老人",@"aaaaaaaaaa",@"sss",@"asdasdaaaaa"];
    moneyArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear: animated];
    [SVProgressHUD dismiss];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    index = 0;
    self.title = @"支付";
    // 0为余额
    btnSender = 0;
    self.BeiZhuTF.delegate = self;
    self.ZhiFuTF.delegate = self;
    self.PassWordTF.delegate = self;
    self.loadUserData = [ReadLoginData readLoginData];
    self.JiFenLabel.text =  [NSString stringWithFormat:@"%.2f",[[self.loadUserData objectForKey:@"gwj_Money"] floatValue]];
    self.ZhangHuyuELabel.text =  [NSString stringWithFormat:@"%.2f",[[self.loadUserData objectForKey:@"Money"] floatValue]];
    self.PassWordTF.secureTextEntry = YES;
    self.ShowLabel.hidden = YES;
}
// 类型
- (IBAction)typeBtn:(id)sender {
    
    _popView = [[Z_PopView alloc] initWithArray:typeArr];
    __weak typeof(self) weakSelf = self;
    _popView.chooseBlock = ^(NSString *chooseItem) {
        _isSelected = NO;
        
         NSArray *b = [chooseItem componentsSeparatedByString:@","];
        [sender setTitle:b[0] forState:UIControlStateNormal];
        
        index = [b[1] integerValue];
        
        
        [weakSelf getMessange];
    };
    [_popView showInView:self.view baseView:sender withPosition:ZShowBottom];
    
  
}

- (void)getMessange{
    
    NSString *money = moneyArr[index];
    NSString *showStr = [NSString stringWithFormat:@"%@%@",@"此品类最高积分可用余额:",moneyArr[index]];
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:showStr];
    
    // 修改价格 颜色
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor orangeColor]
     
                          range:NSMakeRange(9, money.length)];
    self.ShowLabel.attributedText = AttributedStr;

    
}
- (IBAction)FristBtn:(id)sender {
    btnSender = 0;
    // 余额
    [sender setBackgroundColor:[UIColor blueColor]];
    [self.JiFenBtn setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1]];
    self.ShowLabel.hidden = YES;
}


- (IBAction)One:(id)sender {
    
    btnSender = 0;
    // 余额
    [self.ZhangHUBtn setBackgroundColor:[UIColor blueColor]];
    [self.JiFenBtn setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1]];
    self.ShowLabel.hidden = YES;
    
}




- (IBAction)SecBtn:(id)sender {
    btnSender = 1;
    // 积分
    [sender setBackgroundColor:[UIColor blueColor]];
    [self.ZhangHUBtn setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1]];
    self.ShowLabel.hidden = NO;
    
    if ([self.choseBtn.titleLabel.text isEqualToString:@"请选择商品分类"]) {
        self.ShowLabel.text = @"此品类最高积分可用余额:";
    }else{
        NSString *showStr = [NSString stringWithFormat:@"%@%@",@"此品类最高积分可用余额:",moneyArr[index]];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:showStr];
        
        // 修改价格 颜色
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:[UIColor orangeColor]
         
                              range:NSMakeRange(9, [moneyArr[0] length])];
        self.ShowLabel.attributedText = AttributedStr;

        
    }
 
}

- (IBAction)Two:(id)sender {
    
    btnSender = 1;
    // 积分
    [self.JiFenBtn setBackgroundColor:[UIColor blueColor]];
    [self.ZhangHUBtn setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1]];
    self.ShowLabel.hidden = NO;
    
    if ([self.choseBtn.titleLabel.text isEqualToString:@"请选择商品分类"]) {
        self.ShowLabel.text = @"此品类最高积分可用余额:";
    }else{
        NSString *showStr = [NSString stringWithFormat:@"%@%@",@"此品类最高积分可用余额:",moneyArr[index]];
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:showStr];
        
        // 修改价格 颜色
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:[UIColor orangeColor]
         
                              range:NSMakeRange(9, [moneyArr[0] length])];
        self.ShowLabel.attributedText = AttributedStr;
        
        
    }
}

- (IBAction)ActionBtn:(id)sender {
    [self getData];
}
// 加载用户数据
- (void)loadNewData {
    NSDictionary *dict = @{
                           @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                           @"Password":[ReadLoginData readLoginData][@"Password"]
                           };

    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
        NSDictionary *loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];
        self.JiFenLabel.text =  [NSString stringWithFormat:@"%.2f",[[loginDict objectForKey:@"gwj_Money"] floatValue]];
        self.ZhangHuyuELabel.text =  [NSString stringWithFormat:@"%.2f",[[loginDict objectForKey:@"Money"] floatValue]];
         [SVProgressHUD dismiss];
    }];
    
    
}

- (void)getData{
    
    if (self.ZhiFuTF.text.length == 0  || self.PassWordTF.text.length == 0  || self.BeiZhuTF.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alert show];
        return;
    }
    if ([self.choseBtn.titleLabel.text isEqualToString:@"请选择商品分类"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alert show];
        return;
    }
    
    if (btnSender == 0) {
        // 余额
        
        if (self.ZhiFuTF.text.integerValue > [[NSString stringWithFormat:@"%.2f",[[self.loadUserData objectForKey:@"Money"] floatValue]] integerValue]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"账户余额不足" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            
            [alert show];
            return;
        }
    }else{
        //积分
        if (self.ZhiFuTF.text.integerValue > [moneyArr[index] integerValue]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"超过最高积分可用额" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            
            [alert show];
            return;
            
        }
        
    }
    
    
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dic];
    [dic setObject:self.ZhiFuTF.text forKey:@"TurnMoney"];
    [dic setObject:self.PassWordTF.text forKey:@"FromPassword"];
    [dic setObject:[NSString stringWithFormat:@"%ld",(long)btnSender] forKey:@"TurnType"];
    [dic setObject:self.BeiZhuTF.text forKey:@"Remark"];
    
 
     [SVProgressHUD show];
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dic AndMenthodName:@"TURNTOZHUBAO" completeBlock:^(id object) {
    
        if ([[object[@"Id"] stringValue] isEqualToString:@"0"]) {
            // 成功
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
            alert.tag = 1001;
             [SVProgressHUD dismiss];
            
        }else{
            // 失败
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
            [self loadNewData];
            
        }
        
    }];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 12138|| textField.tag == 12139||textField.tag == 12137) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 100 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    self.view.frame =CGRectMake(0, 64, width, height);
    //    NSLog(@"%@", textField.text);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignFitstResponedTextField];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 1001) {
        self.tabBarController.selectedIndex = 0;
        [self.navigationController popToRootViewControllerAnimated:NO];
     
        
        
    }
    
}


- (void)resignFitstResponedTextField {
    [self.BeiZhuTF resignFirstResponder];
    [self.ZhiFuTF resignFirstResponder];
    [self.PassWordTF resignFirstResponder];

    
}
@end
